/*
 * @Author: 0x9DEFA478
 * @Date: 2021-10-23 23:45:32
 * @LastEditTime: 2024-08-18 22:08:09
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "app_main.h"
#include "H_ThreadScheduler.h"
#include "Peripheral.h"



H_TS_Thread* Serial_Thread_Handle;

static int IsCMD(Hbyte* Buffer,int BufferLength,char* CMD){

  while(BufferLength>0){
    BufferLength--;

    if(CMD[0]=='\0'){
      return 0;
    }

    if(CMD[0]!=Buffer[0]){
      return 0;
    }

    CMD=&CMD[1];
    Buffer=&Buffer[1];
  }

  return -1;
}

int Serial_Thread(void* v){
  int length;
  int MaxLength;
  Hbyte* str;

  unsigned char Buffer[128];
  H_Malloc_Info info;
  H_TS_ThreadInfo* thread_info;
  H_TS_ThreadInfo* thread_info_item;

  MaxLength=sizeof(Buffer);

  

  for(;;){

    str=Buffer;
    
    length=I_DebugPort_Read(Buffer,1);
    if(length<=0){
      continue;
    }

    if(str[0]==' '){//指令标识符

      length=I_DebugPort_Read(Buffer,MaxLength);
      if(length<=0){
        continue;
      }


      if(0!=IsCMD(str,length,"GetStackInfo")){

        MemoryMalloc_DTCM.GetInfo(&info);
        H_Printf("DTCM:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
          ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
        MemoryMalloc_D2SRAM.GetInfo(&info);
        H_Printf("D2SRAM:{f.2}%\r\n  Result:{i}\r\n  可使用:{i}Byte\r\n  总共:{i}Byte\r\n",100.0f-(float)info.FreeSize*100/(info.NoOccupySize+info.OccupySize)
          ,info.Result,info.FreeSize,info.NoOccupySize+info.OccupySize);
        
        H_Printf("RTOS堆栈占用率:{f.2}%\r\n",(float)H_TS_GetStackUtilization()/10.0f);
        

        H_Printf("线程堆栈:{i}Byte\r\n",H_TS_GetThisThread()->Stack.MaxUsage);


        H_Printf("CPU占用率:{f.2}%\r\n",(float)H_TS_GetCPU_Load()/10.0f);

      }else if(0!=IsCMD(str,length,"GetThreadInfo")){

        thread_info=H_TS_ThreadInfoListGet();

        thread_info_item=thread_info;
        while(thread_info_item!=NULL){
          
          const char* ThreadName=thread_info_item->ThreadName;
          if(ThreadName==NULL){
            ThreadName="<未命名>";
          }

          H_Printf("线程信息:\r\n  线程名:{s}\r\n  优先级:{i}\r\n  堆栈占用:{i}/{i}={f.2}%\r\n\r\n",ThreadName,thread_info_item->Priority,thread_info_item->StackMaxUsage,thread_info_item->StackSize,100.0f*(float)thread_info_item->StackMaxUsage/thread_info_item->StackSize);
          
          thread_info_item=thread_info_item->Next;
        }

        H_TS_ThreadInfoListFree(thread_info);


      }else{
        H_Printf("无效指令\r\n");
      }


    }

  }
}



extern void stressTest(void);

//主线程函数, 不要在其他地方调用这个
int app_main(void* v){
  H_TS_ThreadAttribute ThreadAttribute;

  ThreadAttribute.Priority=vapp_main_Priority+1;
  ThreadAttribute.StackSize=2048;
  ThreadAttribute.ThreadName="Serial_Thread";
  Serial_Thread_Handle=H_TS_StartNewThread(Serial_Thread,NULL,&ThreadAttribute);
  if(cH_IsNull(Serial_Thread_Handle)){
    for(;;){
    }
  }

  stressTest();
  
  return 0;
}



